﻿using System;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Bricks.EnterpriseLibrary.Validation
{

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TValidatorFactory"></typeparam>
    public abstract class ValidatorFacadeFactoryBase<TValidatorFactory> : FacadeBase<TValidatorFactory>, Bricks.Validation.IValidatorFacadeFactory where TValidatorFactory : ValidatorFactory
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="validatorFactoryAccessor"></param>
        protected ValidatorFacadeFactoryBase(Func<TValidatorFactory> validatorFactoryAccessor) :
            base(validatorFactoryAccessor)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public Bricks.Validation.IValidatorFacade Create(Type targetType)
        {
            MethodInfo createMethodInfo = GetType().GetMethod("Create", new Type[] { }).MakeGenericMethod(targetType);

            return createMethodInfo.Invoke(this, new object[] { }) as Bricks.Validation.IValidatorFacade;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <returns></returns>
        public virtual Bricks.Validation.IValidatorFacade<TTarget> Create<TTarget>()
        {
            return Execute<Bricks.Validation.IValidatorFacade<TTarget>>(validatorFactory => new ValidatorFacade<TTarget>(validatorFactory.CreateValidator<TTarget>()));
        }
    }
}
